const express = require("express");
const router = express.Router();
const {success, successWithData} = require("../utils/result")
const { check } = require('express-validator');
const {checkParam}  = require("../utils/checkUtils")
const BlogTopic = require("../db/model/blogTopicModel");
const BlogCategory = require("../db/model/blogCategoryModel");
const { Op } = require("sequelize");

/**
 * 分页查询接口
 * @api {POST} /blogTopic/queryBlogTopicListByPage 分页查询接口
 * @apiDescription 分页查询接口
 * @apiName queryBlogTopicListByPage
 * @apiHeader {String} authorization 用户token,格式：Bearer xxxx
 * @apiBody {Number} page=1   页码
 * @apiBody {Number} limit=10   每页条数
 * @apiParamExample {json} Request-Example:
 *     {
 *        "page": 1,
 *        "limit": "10",
 *     }
 * @apiGroup BlogTopic
 * @apiVersion 1.0.0
 */
router.post("/blogTopic/queryBlogTopicListByPage", check('page').not().isEmpty(),check('limit').not().isEmpty(), async function (req, res) {
  checkParam(req)
  const { page, limit, name } = req.body;
  const where = {};
  let offset = (page - 1) * limit;
  if (name) {
    where["name"] = {
      [Op.like]: "%" + name + "%",
    };
  }
  const result = await BlogTopic.findAndCountAll({
    limit,
    offset,
    where,
    order: [["created_time", "DESC"]],
  });
  if (result.count > 0) {
    let cateIdList = result.rows.map((topic) => topic.cid);
    let blogCates = await BlogCategory.findAll({
      where: { id: { [Op.in]: cateIdList } },
    });
    if (blogCates && blogCates.length > 0) {
      let map = new Map();
      blogCates.forEach((row) => {
        map[row.id] = row.name
      });
      result.rows.forEach((item) => {
        item.dataValues.cname = map[item.cid];
      });
      console.log(result);
    }
  }

  return res.json(successWithData(result));
});


/**
 * @api {POST} /blogTopic/add 添加
 * @apiDescription 添加
 * @apiName add
 * @apiHeader {String} authorization 用户token,格式：Bearer xxxx
 * @apiBody {Number} id=1   
 * @apiBody {String} name=''   
 * @apiBody {Number} uid=1   
 * @apiBody {Number} cid=1   
 * @apiBody {Number} status=1   
 * @apiBody {Number} view_count=1   
 * @apiBody {Number} like_count=1   
 * @apiBody {String} content=''   
 * @apiBody {String} created_time='1994-04-01'   
 * @apiBody {String} updated_time='1994-04-01'   
 * @apiBody {Number} comment_count=1   
 * @apiBody {String} uname=''   
 * @apiBody {String} desc=''   
 * @apiBody {Number} is_top=1   
 * @apiBody {String} head_img=''   
 * @apiParamExample {json} Request-Example:
 *     {
 *        "id": 1,
 *        "name": '',
 *        "uid": 1,
 *        "cid": 1,
 *        "status": 1,
 *        "view_count": 1,
 *        "like_count": 1,
 *        "content": '',
 *        "created_time": '1994-04-01',
 *        "updated_time": '1994-04-01',
 *        "comment_count": 1,
 *        "uname": '',
 *        "desc": '',
 *        "is_top": 1,
 *        "head_img": '',
 *     }
 * @apiGroup BlogTopic
 * @apiVersion 1.0.0
 */
router.post("/blogTopic/add",  async function (req, res) {
  await BlogTopic.create({...req.body});
  return res.json(success());
});


/**
 * @api {POST} /blogTopic/edit 编辑
 * @apiDescription 编辑
 * @apiName edit
 * @apiHeader {String} authorization 用户token,格式：Bearer xxxx
 * @apiBody {Number} id=1   
 * @apiBody {String} name=''   
 * @apiBody {Number} uid=1   
 * @apiBody {Number} cid=1   
 * @apiBody {Number} status=1   
 * @apiBody {Number} view_count=1   
 * @apiBody {Number} like_count=1   
 * @apiBody {String} content=''   
 * @apiBody {String} created_time='1994-04-01'   
 * @apiBody {String} updated_time='1994-04-01'   
 * @apiBody {Number} comment_count=1   
 * @apiBody {String} uname=''   
 * @apiBody {String} desc=''   
 * @apiBody {Number} is_top=1   
 * @apiBody {String} head_img=''   
 * @apiParamExample {json} Request-Example:
 *     {
 *        "name": '',
 *        "uid": 1,
 *        "cid": 1,
 *        "status": 1,
 *        "view_count": 1,
 *        "like_count": 1,
 *        "content": '',
 *        "created_time": '1994-04-01',
 *        "updated_time": '1994-04-01',
 *        "comment_count": 1,
 *        "uname": '',
 *        "desc": '',
 *        "is_top": 1,
 *        "head_img": '',
 *     }
 * @apiGroup BlogTopic
 * @apiVersion 1.0.0
 */
router.post("/blogTopic/edit", check('id').not().isEmpty(),async function (req, res) {
  checkParam(req)
  await BlogTopic.update({...req.body}, {where: {id:req.body.id}});
  return res.json(success());
});

/**
 * @api {POST} /blogTopic/del 删除
 * @apiDescription 删除
 * @apiName del
 * @apiHeader {String} authorization 用户token,格式：Bearer xxxx
 * @apiBody {Number} id=1   主键id
 * @apiParamExample {json} Request-Example:
 *     {
 *        "id": 1,
 *     }
 * @apiGroup BlogTopic
 * @apiVersion 1.0.0
 */
router.post("/blogTopic/del", check('id').not().isEmpty(),async function (req, res) {
  checkParam(req)
  await BlogTopic.destroy({where: {id:req.body.id}});
  return res.json(success());
});
module.exports = router;